//
//  GQJCopyController.m
//  TotalDemo
//
//  Created by gujian on 16/8/1.
//  Copyright © 2016年 gujian. All rights reserved.
//

#import "GQJCopyController.h"
#import "TestRumtime.h"
#import "GQJCompareModel.h"
#import "GQJWeakController.h"

OBJC_EXTERN int _objc_rootRetainCount(id);


@interface GQJCopyController()


@end

@implementation GQJCopyController


- (void)viewDidLoad
{
    [super viewDidLoad];
    
    GQJWeakController *controller = [[GQJWeakController alloc] init];
    controller.view.backgroundColor = [UIColor blackColor];
    controller.view.backgroundColor = [UIColor orangeColor];

//    NSString *originStr = @"123";
//    
//    NSLog(@"originStr.retainCount:%d %p",_objc_rootRetainCount(originStr), originStr);
//    
//    NSString *simpleCpy = originStr;
//    NSLog(@"originStr.retainCount:%d %p",_objc_rootRetainCount(originStr),simpleCpy);
//    
//    NSString *deepCpy = [originStr copy];
//    NSLog(@"originStr.retainCount:%d %p",_objc_rootRetainCount(originStr), deepCpy);
//    
//    
//    ///
////    NSObject *ob = [[NSObject alloc] init];
////    NSLog(@"ob.retainCount:%d",_objc_rootRetainCount(ob));
////    
////    NSObject *ob1 = ob;
////    NSLog(@"ob.retainCount:%d",_objc_rootRetainCount(ob));
//    
//    originStr = @"456";
//    NSLog(@"%@originStr.retainCount:%d %p", originStr,_objc_rootRetainCount(originStr), originStr);
//    NSLog(@"%@originStr.retainCount:%d %p",simpleCpy,_objc_rootRetainCount(originStr),simpleCpy);
//    NSLog(@"%@originStr.retainCount:%d %p",deepCpy,_objc_rootRetainCount(originStr), deepCpy);
//    
//    
//    NSMutableString *str = [[NSMutableString alloc] initWithFormat:@"abc"];//[NSMutableString stringWithFormat:@"abc"];
//    NSLog(@"%@originStr.retainCount:%d %p",str,_objc_rootRetainCount(str), str);
//    
//    NSString *musimpleCpy = str;
//    NSLog(@"%@originStr.retainCount:%d %p",musimpleCpy,_objc_rootRetainCount(str),musimpleCpy);
//    
//    NSString *mudeepCpy = [str copy];
//    NSLog(@"%@originStr.retainCount:%d %p",mudeepCpy,_objc_rootRetainCount(str), mudeepCpy);
//    
//    
//    [str appendFormat:@"456"];
//    NSLog(@"%@originStr.retainCount:%d %p", str,_objc_rootRetainCount(str), str);
//    NSLog(@"%@originStr.retainCount:%d %p",musimpleCpy,_objc_rootRetainCount(str),musimpleCpy);
//    NSLog(@"%@originStr.retainCount:%d %p",mudeepCpy,_objc_rootRetainCount(str), mudeepCpy);
 
    
    NSString *lightStr = [NSString stringWithFormat:@"light Copy"];
    
    TestRumtime *testRunTime = [[TestRumtime alloc] init];
    testRunTime.name = lightStr;
    NSLog(@"origin lightStr:%@->%p",lightStr,lightStr);
    lightStr = @"修改值";
    NSLog(@"alter lightStr:%@->%p",lightStr,lightStr);
    NSLog(@"testRunTime.name:%@->%p",testRunTime.name,testRunTime.name);
    
    
    NSMutableString *deepStr = [[NSMutableString alloc] initWithString:@"deep Copy"];
    
    TestRumtime *deepTestRuntime = [[TestRumtime alloc] init];
    deepTestRuntime.name = deepStr;
    
    NSLog(@"origin deepStr:%@->%p",deepStr, deepStr);
    [deepStr appendString:@" Test"];
    NSLog(@"alter deepStr:%@->%p",deepStr, deepStr);
    NSLog(@"testRunTime.name:%@->%p",deepTestRuntime.name,deepTestRuntime);
    
    
    /**
     *  使用copy的原因为，保留对象的一份全新的副本，避免数据被错误的修改
     */
    
    NSString *str1 = @"123";
    __unused NSString *str2 = [str1 copy];//与str2 = str1;效果一致，执行结果仅是对引用对象的retainCount+1
    str1 = @"456";//str1指向了新的对象，并未对当前指向的对象进行修改,表明NSString是常量，不能进行修改
    
    
    NSMutableString *deepStr1 = [[NSMutableString alloc] initWithString:@"456"];
    __unused NSString *lightStr2 = deepStr1;
    __unused NSString *deepStr3 = [deepStr1 copy];
    [deepStr1 appendString:@"123"];


    //http://my.oschina.net/pengloo53/blog/173810
    GQJCompareModel *model1 = [[GQJCompareModel alloc] initWithName:@"张三" age:11];
    GQJCompareModel *model2 = [[GQJCompareModel alloc] initWithName:@"李四" age:32];
    GQJCompareModel *model3 = [[GQJCompareModel alloc] initWithName:@"王五" age:2];
    NSArray *arr1 = @[model1,model2,model3];
    [[arr1 reverseObjectEnumerator] allObjects];
    NSArray *arr2 = [arr1 sortedArrayUsingSelector:@selector(comparePerson:)];
    NSLog(@"arr2:%@",arr2);
    
    NSArray *arr3 = [arr1 sortedArrayUsingComparator:^NSComparisonResult(GQJCompareModel *obj1, GQJCompareModel *obj2) {
        
        NSComparisonResult result = [[NSNumber numberWithUnsignedInteger:obj1.age] compare:[NSNumber numberWithUnsignedInteger:obj2.age]];
        
        if (result == NSOrderedSame) {
            result = [obj1.name compare:obj2.name];
        }
        
        return result;
        
    }];
    NSLog(@"arr3:%@",arr3);
    
    NSArray *arr4 = [arr1 sortedArrayUsingFunction:sortWithParm context:nil];
    NSLog(@"arr4:%@",arr4);
    
    //运用了hash的思想
    NSArray *arr5 = [arr1 sortedArrayUsingFunction:sortWithParm context:nil hint:arr1.sortedArrayHint];
    NSLog(@"arr5:%@",arr5);
    
    NSSortDescriptor *sortAgeDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"age" ascending:YES];
    NSSortDescriptor *sortNameDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES];
    
    NSArray *arr6 = [arr1 sortedArrayUsingDescriptors:@[sortAgeDescriptor,sortNameDescriptor]];
    NSLog(@"arr4:%@",arr6);
    
}


NSInteger sortWithParm(id model11, id model22, void *context)
{
    GQJCompareModel *model1 = (GQJCompareModel *)model11;
    GQJCompareModel *model2 = (GQJCompareModel *)model22;
    
    if (model1.age > model2.age) {
        return NSOrderedAscending;
    }else if (model1.age < model2.age)
    {
        return NSOrderedDescending;
    }else
    {
        const char *name1 = [model1.name cStringUsingEncoding:NSUTF8StringEncoding];
        const char *name2 = [model2.name cStringUsingEncoding:NSUTF8StringEncoding];
        int result = strcmp(name1, name2);
        if (result == 0) {
            return NSOrderedSame;
        }else if (result > 0)
        {
            return NSOrderedAscending;
        }else
            return NSOrderedDescending;
    }
    
    
}



@end
